package www.bw.com.controller;



import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import www.bw.com.entities.User;


import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/user")

public class UserController {



  public static List<User> users;

static {
  users = Arrays.asList(

    new User(1, "孙悟空", 1000, "吃蟠桃", new Date()),
    new User(2, "猪八戒", 2000, "调戏嫦娥", new Date()),
    new User(3, "唐僧", 800, "念经", new Date()),
    new User(4, "沙悟净", 1500, "挑行李", new Date()),
    new User(5, "白龙马", 1200, "被骑", new Date()),

    new User(6, "孙悟空", 1000, "吃蟠桃", new Date()),
    new User(7, "猪八戒", 2000, "调戏嫦娥", new Date()),
    new User(8, "唐僧", 800, "念经", new Date()),
    new User(9, "沙悟净", 1500, "挑行李", new Date()),
    new User(10, "白龙马", 1200, "被骑", new Date()),
    new User(11, "孙悟空", 1000, "吃蟠桃", new Date()),
    new User(12, "猪八戒", 2000, "调戏嫦娥", new Date()),
    new User(13, "唐僧", 800, "念经", new Date()),
    new User(14, "沙悟净", 1500, "挑行李", new Date()),
    new User(15, "白龙马", 1200, "被骑", new Date()),
    new User(16, "孙悟空", 1000, "吃蟠桃", new Date()),
    new User(17, "猪八戒", 2000, "调戏嫦娥", new Date()),
    new User(18, "唐僧", 800, "念经", new Date()),
    new User(19, "沙悟净", 1500, "挑行李", new Date()),
    new User(20, "白龙马", 1200, "被骑", new Date())


  );
}
  @GetMapping
  public List<User> list(@RequestParam(value = "cpage", defaultValue = "1") Integer cpage, @RequestParam(value = "row", defaultValue = "3") Integer row) {


    int index=(cpage-1)*row;
    return users.subList(index,index+row);

  }

  public static void main(String[] args) {
    System.out.println(users.subList(3,6));
  }


  @GetMapping("export")
  public void export(HttpServletResponse response){
  exportExcel("abc.xls",
               Arrays.asList("ID","姓名","爱好","年龄","生日"),
    Arrays.asList("id", "name", "hobby", "age", "birthday"),
    users,
    response);
  }

  private void exportExcel(String fileName, List<String> header, List<String> fields, List<?> data, HttpServletResponse response) {

    response.addHeader("Content-Disposition","attachment;filename="+fileName);
    try {
      Workbook wb = new HSSFWorkbook();

      Sheet sheet = wb.createSheet("学生表");
      Row row = sheet.createRow(0);
      for (int i=0;i<header.size();i++){
        Cell cell = row.createCell(i);
        cell.setCellValue(header.get(i));
      }

      //数据
      for (int i=0;i<data.size();i++){
        Row sheetRow = sheet.createRow(i + 1);
        Object o = data.get(i);
        for (int j=0;j<fields.size();j++){
          String fieldName = fields.get(j);
          Cell cell = sheetRow.createCell(j);

          //数据值
          Field field = o.getClass().getDeclaredField(fieldName);
          field.setAccessible(true);
          Object fieldValue = field.get(o);
          cell.setCellValue(fieldValue.toString());
        }
      }
       wb.write(response.getOutputStream());
    }catch (Exception e){
      e.printStackTrace();
    }
  }

}
